package com.yang.booking.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.booking.dao.AppointmentMapper;
import com.yang.booking.dao.AppointmentRuleMapper;
import com.yang.booking.dao.TemporaryArrangeMapper;
import com.yang.booking.pojo.Appointment;
import com.yang.booking.pojo.AppointmentRule;
import com.yang.booking.pojo.TemporaryArrange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AppointmentRuleService {

    @Autowired
    private AppointmentRuleMapper appointmentRuleMapper;

    @Autowired
    private TemporaryArrangeMapper temporaryArrangeMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Transactional
    public Map<String, Object> insertAppointmentRule(String storeId,
                                                     String earliestReserve,
                                                     String latestReserve,
                                                     String latestCancel,
                                                     String mostMeanwhile,
                                                     String mostTemporary){

        HashMap<String, Object> map = new HashMap<>();
        map.put("store_id", storeId);
        int tcount = 0;
        for (TemporaryArrange temporaryArrange: temporaryArrangeMapper.selectByMap(map)
             ) {
            QueryWrapper<Appointment> twrapper = new QueryWrapper<>();
            twrapper.eq("type", 3);
            twrapper.eq("temporary_arrange_id", temporaryArrange.getId());
            twrapper.eq("state",1);
            if (appointmentMapper.selectList(twrapper).size() > 0){
                tcount += 1;
            }
        }

        if (Integer.parseInt(mostTemporary) < tcount){
            Map<String, Object> msg = new HashMap<String, Object>();
            msg.put("massage", "小于已临时预约的数量");
            msg.put("code", 400);
            return msg;
        }

        appointmentRuleMapper.deleteByMap(map);

        AppointmentRule ar = new AppointmentRule();
        ar.setStoreId(storeId);
        ar.setEarliestReserve(Integer.parseInt(earliestReserve));
        ar.setLatestReserve(Integer.parseInt(latestReserve));
        ar.setLatestCancel(Integer.parseInt(latestCancel));
        ar.setMostMeanwhile(Integer.parseInt(mostMeanwhile));
        ar.setMostTemporary(Integer.parseInt(mostTemporary));
        ar.setCreateAt(new Date());
        ar.setUpdateAt(new Date());
        appointmentRuleMapper.insert(ar);

        for (TemporaryArrange temporaryArrange: temporaryArrangeMapper.selectByMap(map)
        ) {
            QueryWrapper<Appointment> twrapper = new QueryWrapper<>();
            twrapper.eq("type", 3);
            twrapper.eq("temporary_arrange_id", temporaryArrange.getId());
            twrapper.eq("state",1);
            if (appointmentMapper.selectList(twrapper).size() == 0){
                temporaryArrangeMapper.deleteById(temporaryArrange.getId());
            }
        }

        for (int j = 1; j <= Integer.parseInt(mostTemporary) - tcount; j++) {
            TemporaryArrange temporaryArrange = new TemporaryArrange();
            temporaryArrange.setName("临时安排");
            temporaryArrange.setStoreId(storeId);
            temporaryArrange.setCreateAt(new Date());
            temporaryArrange.setUpdateAt(new Date());
            temporaryArrangeMapper.insert(temporaryArrange);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

}
